<?php ob_start();
?>
<meta name="viewport" content="width=device-width user-scalable=0" />
<html>
  
  <head>
	 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>    
	<title>Welcome to Yes or No</title>    
	
	  <style type = "text/css">
		  body{
			  font-family: Futura;
			  background-image: gray;
		  }
		  p{
			  color: maroon;
		  }
		  h1{
			  font-family: Zapfino;
			  color: gray;
			  font-size : 40;
			
		  }
	  </style>
	<script src="../googleAnalytics.js" type="text/javascript"></script>   
	 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=ABQIAAAAHHQwI7Yl5wVURxsuvbc5jxR1DuF9CKJIoQnYx8pOlreVCeiNJhQZ3xtJ2dXnNog9lKrvMIeQPVKitA" type="text/javascript"></script>	
	 <script type"text/javascript"> 
		window.onload=initialize;
		window.onunload=GUnload;
		
		var map;
		var geocoder;
		var myStatus;
 
		// Very Basic Map setup on canvas
		
	function view(arg){
     	if(arg=="1"){
          document.all.sub1.style.display="none";
          document.all.sub2.style.display="";
          document.all.sub3.style.display="";
     	}
     	else{
     
          	document.all.sub1.style.display="";
          	document.all.sub2.style.display="none";
          	document.all.sub3.style.display="none";
  		   }
 		}
		function initialize() {      
			if (GBrowserIsCompatible()) {        
				map = new GMap2(document.getElementById("map_canvas"));     
				geocoder = new GClientGeocoder();
				map.setCenter(new GLatLng(37.4419, -122.1419), 13);  
				map.disableDoubleClickZoom();
				map.addControl(new GSmallMapControl());
 
				addEvtListners();
			}    
		}
 
		function addEvtListners() {
 
			myEventListener1 = GEvent.addListener(map, "click", function(overlay, latlng) {    
				if (latlng) {  
					map.clearOverlays();
					geocoder.getLocations(latlng, showAddress);
					myStatus =	"The GPoint: "										+ map.fromLatLngToDivPixel(latlng) + "<br/>" + 
								"The GLatlng: "										+ latlng.toString() + "<br/>" + 
								"Zoom level(0~19): "								+ map.getZoom() + "<br/>" + 
								"<br/>";  
				
				}
			});
 
		}
 
		function showAddress(response) {  
			if (!response || response.Status.code != 200) {    
				alert("Status Code:" + response.Status.code);  
			} 
			else {    
				place = response.Placemark[0];    
				point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
				marker = new GMarker(point);
				map.addOverlay(marker);
 
				document.q.address.value=response.Placemark[0].address; 
				myStatus =response.Placemark[0].address;      
				document.q.xpoint.value=place.Point.coordinates[1]; 
				document.q.ypoint.value=place.Point.coordinates[0];
			}
		}
 
		function refreshStatus() {
			document.getElementById("status").innerHTML = "";	
			counter1=0;
		}
		
	Cats=new Array(4);
	Cats[0]=new Array(3);
	Cats[1]=new Array(2);
	Cats[2]=new Array(5);
	Cats[3]=new Array(4);
 

	Cats[0][0]="-----";
	
 

	Cats[1][0]="Rh+ A";
	Cats[1][1]="Rh+ B";
	Cats[1][2]="Rh+ O";
	Cats[1][3]="Rh+ AB";
	Cats[1][4]="Rh- A";
	Cats[1][5]="Rh- B";
	Cats[1][6]="Rh- O";
	Cats[1][7]="Rh- AB";
 
	Cats[2][0]="-----";
	

	Cats[3][0]="Clothes";
	Cats[3][1]="Foods";
	Cats[3][2]="Etc";
	

	function BuildCats(num)
	{
     
      document.q.ChannelCats.selectedIndex=0;
      
     
      for(ctr=0;ctr<Cats[num].length;ctr++)
      {
          document.q.ChannelCats.options[ctr]=new Option(Cats[num][ctr],Cats[num][ctr]);
      }
    
      document.q.ChannelCats.length=Cats[num].length;
	}
 	
 	function makeDay(form) {
		var YEAR=form.year.options[form.year.selectedIndex].value;
		var MONTH=form.month.options[form.month.selectedIndex].value;
		var daysInMonth=new Date(new Date(YEAR,MONTH,1)-86400000).getDate();
		for(var i=0; i.day.length; i++) form.day.options[i]=null;
		for(var j=0; j<daysInMonth; j++) {
		if(j<9) var k="0"+(j+1); else var k=j+1;
			form.day.options[j]=new Option(k);
		}
	}
	function setDefaultDate(form) {
		var current=new Date();
		var YEAR=current.getFullYear(); var MONTH=current.getMonth()+1;
		var daysInMonth=new Date(new Date(YEAR,MONTH,1)-86400000).getDate();
		for(var i=0; i<daysInMonth; i++) {
		if(i<9) var j="0"+(i+1); else var j=i+1;
			document.write("<option value="+j+">"+j);
		}
		form.year.value=YEAR;
		form.month.options[MONTH-1].selected=true;
		form.day.options[current.getDate()-1].selected=true;
	}
	
	window.onorientationchange = function() {
  
  var orientation = window.orientation;
  switch(orientation) {
    case 0:
       
        document.body.setAttribute("class","portrait");
       
       
        document.getElementById("currentOrientation").innerHTML="Now in portrait orientation (Home button on the bottom).";
        break; 
       
    case 90:
       
        document.body.setAttribute("class","landscapeLeft");
       
        document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the left (Home button to the right).";
        break;
   
    case -90: 
       
        document.body.setAttribute("class","landscapeRight");
       
        document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the right (Home button to the left).";
        break;
  }
}








	</script> 
  </head>
  <body>

    <h1>Send</h1>
    <?php
    
    $output_form = false;	//html 폼을 출력할지 말아야할지를 결정하는 변수
	   //message
    $message = NULL;	
		$reciever =NULL; 
    $subject = NULL; 
		
    //date
    $year = NULL;
    $month = NULL;
    $day = NULL;
    $time = NULL;
  
    //category
    $category = NULL;
    $category2= NULL;
    
    //location
    $location_x = NULL;
    $location_y = NULL;
    $address = NULL;
    $maximum = NULL;
  
    
    if( isset( $_POST['submit'] ) ){
       //message
      $message = $_POST[ 'message' ];
      $reciever = $_POST['reciever'];
      $subject = $_POST['subject'];
      
      //date
      $year = $_POST['year'];
      $month = $_POST['month'];
      $day = $_POST['day'];
      $time = $_POST['time'];
     
      //location 
      $location_x = $_POST['xpoint'];
      $location_y = $_POST['ypoint'];
      $address = $_POST['address'];   
      
      //count
      $maximum = $_POST['maximum'];
      
      if( empty( $subject ) ){
     		echo 'You forgot the subject.<br />';
				$output_form = true;      
      }
      if( empty( $message ) ){
     		echo 'You forgot the message.<br />';
				$output_form = true;
      }
    }else{
      $output_form = true;
    }
    
          
   
   if( $output_form ){
    ?>
	 <form name="q" action = " <?php  echo $_SERVER['PHP_SELF'];  ?> " method="post" > 
		 <table>
          <tr>
            <td>
              <p>To:</p>
            </td>
            <td>
              <input type="text" id ="reciever" name="reciever" value="" size="30"/>
            </td>
          </tr>
          <tr>
            <td>
              <p>Subject:</p>
            </td>
            <td>
              <input type="text" id ="subject" name="subject" value= "<?php  echo $subject;  ?>" size="30"/>
            </td>
          </tr>
           <tr>
            <td>
              <p>Maximum:</p>
            </td>
            <td>
            	<SELECT name="maximum" id="maximum" >
					<option value=1>1</option>
					<option value=2>2</option>
					<option value=3>3</option>
					<option value=4>4</option>
					<option value=5>5</option>
					<option value=6>6</option>
					<option value=7>7</option>
					<option value=8>8</option>
					<option value=9>9</option>
					<option value=10>10</option>
					<option value=11>11</option>
					<option value=12>12</option>
					<option value=13>13</option>
					<option value=14>14</option>
					<option value=15>15</option>
					<option value=16>16</option>
					<option value=17>17</option>
					<option value=18>18</option>
					<option value=19>19</option>
					<option value=20>20</option>
				</SELECT>
            </td>
          </tr>
           <tr>
            <td>
              <p>Date:</p>
            </td>
            <td>
				<SELECT name="year" id="year" onChange="makeDay(this.form)">
					<option value=2011>2011</option>
					<option value=2012>2012</option>
					<option value=2013>2013</option>
					<option value=2014>2014</option>
					<option value=2015>2015</option>
					<option value=2016>2016</option>
					<option value=2017>2017</option>
				</SELECT>/
				<SELECT name="month" id="month" onChange="makeDay(this.form)">
					<option value=01>01<option value=02>02<option value=03>03<option value=04>04
					<option value=05>05<option value=06>06<option value=07>07<option value=08>08
					<option value=09>09<option value=10>10<option value=11>11<option value=12>12
				</SELECT>
				<SELECT name="day" id="day">
					<script>setDefaultDate(document.form)</script>
				</SELECT>
				/
				<SELECT name="time" id="time" >
					<option value=01>01<option value=02>02<option value=03>03<option value=04>04
					<option value=05>05<option value=06>06<option value=07>07<option value=08>08
					<option value=09>09<option value=10>10<option value=11>11<option value=12>12
					<option value=13>13<option value=14>14<option value=15>15<option value=16>16
					<option value=17>17<option value=18>18<option value=19>19<option value=20>20
					<option value=21>21<option value=22>22<option value=23>23<option value=12>24
				</SELECT>
			 
            </td>
          </tr>
           <tr>
            	<td><p>Category: </p></td>
            	<td>
				
						<SELECT Name="ChannelNames" id="category1" OnChange="BuildCats(this.selectedIndex);">
     						<OPTION Value="news">Medical Aid
 			    			<OPTION Value="ebusiness">Blood donation
							<OPTION Value="web">Voluntary work
							<OPTION Value="tech">Material
						</Select>
						&nbsp;&nbsp;
				
						<SELECT Name="ChannelCats" id="category2">
      						<OPTION Value="">--------
							</Select>
            	</td>
          </tr>
          <tr>
            <td>
              <p>Message:</p>
            </td>
            <td>
              <textarea name="message" id="message" rows="10" cols="30"  ><?php echo $message;  ?></textarea>
            </td>
          </tr>
        </table>
 			<table>
 				<tr id ="sub1">
 					<td onclick="javascript:view('1')">
 						<p>Open Map</p>
 					</td>
  				</tr>
  				<tr id ="sub2" style="display:none">
  					<td onclick="javascript:view('2')">
  						<p>Close Map</p>
  					</td>
  				</tr>
  				<tr id ="sub3" style="display:none">
  					<td>
						<div id="map_canvas" style="margin: 1em 1em; width:300px; height: 300px;"></div> 
					</td>
				</tr>	
			</table>
			<table>
				<tr>
					<td>
						<p>Address:</p>
					</td>
					<td>
						<textarea name="address" id="address" rows="2" cols="30"  ></textarea>
					</td>
				</tr>
				<tr>
					<td>
						<p>Glatlng:</p>
					</td>
					<td>

							<input type="text" id="xpoint" name="xpoint" value="">
							<input type="text" id="ypoint" name="ypoint" value="">
						
					</td>
				</tr>	
			</table>		
			
			
        <input type="submit" value="submit" name="submit" />
      </form>
    	
    <?php
    }else{
 			$dbc = mysqli_connect( 'localhost' ,'root' , '', 'teresa' ) or die( 'Error connecting to MySQL server.');
			
      $sender = $_COOKIE[ 'user_email' ];
      $user_id = $_COOKIE[ 'user_id'];
      // insert message data
        echo '<p>' . $address . '</p>';
			$query = "INSERT INTO teresa_message( sender, reciever, subject, message ,maximum , address, location_x, location_y )".
	  		"VALUES( '$sender' , 'sender' , '$subject' ,'$message' ,'$maximum'  ,'$address' , $location_x, $location_y)"; 
      mysqli_query( $dbc, $query ) or die('ERROR INSERT MESSAGE queerying database.');
      
      // select lately data
      $query = "SELECT * FROM teresa_message WHERE sender = '$sender' ORDER BY message_id DESC";
	  	$data = mysqli_query( $dbc , $query) or die('ERROR SELECT queerying database.');
      
      $row = mysqli_fetch_array( $data );
      
      $maximum_count = $row['maximum'];
      $message_id = $row['message_id'];
      $user_id = $_COOKIE[ 'user_id' ];
      
      // linked meessage and sender
    
      $query = "INSERT INTO teresa_response( maximum, user_id, message_id )".
	    	"VALUES( '$maximum' , '$user_id' , '$message_id' )"; 
      mysqli_query( $dbc, $query ) or die('ERROR INSERT RESPONSE queerying database.');     
      
      // find response id
      $query = "SELECT * FROM teresa_response WHERE message_id = '$message_id' ORDER BY message_id DESC";
	  	$data = mysqli_query( $dbc , $query) or die('ERROR find response id queerying database.');

      $row = mysqli_fetch_array( $data );

      $response_id = $row['response_id'];       
      $maximum = $row['maximum'];
      $response = $row['response'];
      
      $query = "UPDATE teresa_message SET response_id = '$response_id' WHERE message_id = '$message_id'";	
			mysqli_query( $dbc , $query) or die('ERROR set response queerying database.');
            
      //close database
      mysqli_close($dbc);	
      $home_url = 'http://' . $_SERVER['HTTP_HOST'] . '/sender_reciever.php?message_id=' . $message_id . '&user_id=' . $user_id . '&response_id=' . $response_id . '&send_Count=' . $maximum . '&response=' . $response;
		  header('Location:' . $home_url );         
    }
    ?>
    
  </body>
	
</html>	